module Api
  class UserController < ApplicationController
    include Apiable

    def verify
      user = User.select(:id).find_or_create_by(mobile: params[:mobile],
                                                user_type: USER_TYPE["用户"])
      if user.blank?
        error! "用户不存在"
      elsif params[:captcha] != redis_get("sms_captcha:#{params[:mobile]}")
        error! "验证码错误"
      else
        authentication_token = generate_authentication_token
        redis_set(authentication_token, user.id, ex: SIGN_IN_TOKEN_EXPIRE_TIME)
        data! token: authentication_token
      end
    end

    def nickname
      if params[:nickname].blank?
        error! "昵称不能为空"
      else
        User.update(api_current_user.id, nickname: params[:nickname])
        data!
      end
    end
  end
end